package com.busfm.activity;

import java.io.File;

import com.busfm.R;
import com.busfm.util.FileUtil;
import com.feedback.UMFeedbackService;
import com.mobclick.android.MobclickAgent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.Toast;

public class SettingActivity extends BaseActivity implements OnClickListener {

    private LinearLayout llClearCache;
    private LinearLayout llAboutUs;
    private LinearLayout llFeedback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        setupView();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    private void initView() {
        llClearCache = (LinearLayout) findViewById(R.id.activity_setting_ll_clear_cache);
        llAboutUs = (LinearLayout) findViewById(R.id.activity_setting_ll_about_us);
        llFeedback = (LinearLayout) findViewById(R.id.activity_setting_ll_feedback);
    }

    private void setupView() {
        llClearCache.setOnClickListener(this);
        llAboutUs.setOnClickListener(this);
        llFeedback.setOnClickListener(this);
    }

    private void clearCache() {
        clearImageCache();
        File[] files = getCacheDir().listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                deleteDir(file);
            }
            file.delete();
        }
        Toast.makeText(this, getString(R.string.activity_settting_clear_success), Toast.LENGTH_SHORT).show();
    }

    private void clearImageCache() {
        File file = new File(FileUtil.getCoverDir());
        if (file.exists()) {
            deleteDir(file);
        }
    }

    private void deleteDir(File dir) {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                deleteDir(file);
            }
            file.delete();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_setting_ll_clear_cache :
                clearCache();
                break;
            case R.id.activity_setting_ll_about_us :
                Intent intent = new Intent();
                intent.setClass(this, AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_setting_ll_feedback :
                UMFeedbackService.openUmengFeedbackSDK(this);
                break;
            default:
                break;
        }
    }
}
